package com.price.common.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.price.common.extend.doCustomCrashListener

abstract class BaseFragment : Fragment {

    private var consData: Array<out Any>? = null

    public constructor() : super()
    constructor(vararg any: Any) : this() {
        consData = any
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(getViewId(), container, false)
    }

    abstract fun getViewId(): Int

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        runCatching {
            initView(view, savedInstanceState)
            super.onViewCreated(view, savedInstanceState)
            initData(savedInstanceState)
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }

    abstract fun initView(view: View, savedInstanceState: Bundle?)
    abstract fun initData(savedInstanceState: Bundle?)

    override fun onResume() {
        super.onResume()
        runCatching {
            resume()
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }

    override fun onPause() {
        super.onPause()
        runCatching {
            pause()
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }
    open fun resume(){}
    open fun pause(){}

}
